package kindergarden.at.google;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/**
 *
 * 
 * @author paja0902 2012-01-31
 * 
 * description: This is a GUI that will show a form for adding a new child to the database
 *  
 *  
 * 
 */
public class form extends JFrame implements ActionListener
{

  private       static JPanel panel;
  private       JButton submit;
  private       JTextField userInput [];

    // constructor
    public form()
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");            
        }
        catch(Exception e)
        {
            System.err.println("Could not load UIManager");
        }

        createFormGUI();
        this.setTitle("Nytt barn");
        this.setVisible(true);
        this.setSize(250, 500);
    }


    private void createFormGUI()
    {           
   

        String[] labels = {"Namn:", "Personnummer:", "Telefonnummer 1:", "Telefonnummer 2:", "Kommentar", "Närvarotid:",
            "", "Namn", " Email","Mobilnummer", "Arbetsplats","Jobbnummer"};
                    
        userInput = new JTextField [] {new JTextField(10), new JTextField(10),new JTextField(10),new JTextField(10),
                new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10),
                new JTextField(10),new JTextField(10),new JTextField(10),new JTextField(10)};
                    
        panel = new JPanel(new SpringLayout());
            
        //Prints the labels and the textfields
        for(int i = 0; i < labels.length; i++)
        {
            JLabel label;
            label = new JLabel(labels[i], JLabel.TRAILING);
            label.setLabelFor(userInput[i]);
            
            if(i ==6 )
                userInput[i].setVisible(false);

            panel.add(label);
            panel.add(userInput[i]);
        }
                                       
        //panel,numberOFLabels, rows, cols, initX, initY, xPad, yPad
        SpringUtilities.makeCompactGrid(panel, labels.length,2, 6, 6, 6, 6);

        submit = new JButton("Skapa");
        submit.addActionListener(this);

        JPanel panel2 = new JPanel();
        panel2.add(submit);

        this.add(panel, BorderLayout.WEST);
        this.add(panel2, BorderLayout.SOUTH);
 
    }

    public void actionPerformed(ActionEvent e)
    {

        //Creates a child and caretaker from the form input
        //and inserts it to database.
        if(e.getSource() == submit)
        {
            Child c = new Child();
            c.setName(userInput[0].getText());
            c.setSin_num(userInput[1].getText());
            c.setHome_phone_number(userInput[2].getText());
            c.setHome_phone_number2(userInput[3].getText());
            c.setComment(userInput[4].getText());

//            Caretaker ct = new Caretaker();
//            ct.setName(userInput[7].getText());
//            ct.setEmail(userInput[8].getText());
//            ct.setMobile_num(userInput[9].getText()); 
//            ct.setWorkplace(userInput[10].getText());
//            ct.setPhone_number_work(userInput[11].getText());
//            ct.setPhone_number_work(userInput[12].getText()); 

            SQLHandler sh = new SQLHandler();
            sh.insertChild(c);
//            sh.insertCaretaker(ct);

        }
    }

}
